package cputils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Connection;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import common.Utils;

public class CPHMTBService {
	
//	private static DateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");

	public static void main(String[] args) throws IOException {
		CPHMTBService service = new CPHMTBService();
		//writeAicaiDLTHMData("14026");
	//	writeAicaiSSQHMData("2014025");
//		service.writeTaobaoHMData("SSQ","2014028",false);
//		writeTaobaoHMData("SSQ","2014027",false);
//		writeTaobaoHMData("SSQ","2014026",false);
		/*for (int i = 2014031; i <= 2014031; i++) {
			Thread t = new Thread(new ServiceThread("SSQ",i+"",false));
			t.start();
		}*/
		for (int i = 2014031; i <= 2014031; i++) {
			Thread t = new Thread(new ServiceThread("SSQ",i+"","",true));
			t.start();
		}
		//service.readDetailLinkTaobao("SSQ", "2014031", 1, "", true);
		
//		service.writeTaobaoHMData("DLT","14030",true);
		service.tjHM4Taobao("2014031");
	}
	public Map<String, Map<Integer, Integer>> tjHM4Taobao(String issueNo) throws IOException{
		Map<String, Map<Integer, Integer>> fullMap = tjHM4Taobao("tb_"+issueNo+"_full",2);
		System.out.println(fullMap);
		Map<String, Map<Integer, Integer>> notfullMap = tjHM4Taobao("tb_"+issueNo+"_notfull",1);
		System.out.println(notfullMap);
		
		Map<String, Map<Integer, Integer>> map = tjHM4Taobao("tb_"+issueNo+"_",100);
		System.out.println(map);
		return map;
	}
	private Map<String, Map<Integer, Integer>> tjHM4Taobao(String fileName,int is_not_full) throws IOException{
		Map<String, Map<Integer, Integer>> res = new HashMap<String, Map<Integer,Integer>>();
		List<String> list = new ArrayList<String>();
		if (is_not_full == 100) {
			list = XmlUtils.getHMData(fileName+"notfull");
			list.addAll(XmlUtils.getHMData(fileName+"full"));
			fileName = fileName+"all";
		}else {
			list = XmlUtils.getHMData(fileName);
		}
		Map<Integer, Integer> bMap = new HashMap<Integer, Integer>();
		Map<Integer, Integer> aMap = new HashMap<Integer, Integer>();
		for (int i = 1; i <= 35; i++) {
			bMap.put(i, 0);
		}
		for (int i = 1; i <= 16; i++) {
			aMap.put(i, 0);
		}
		for (String s : list) {
			//s = XmlUtils.replaceHanzi(s, ",");
			//s = s.replaceAll(" ", "");
			String[] sArr = s.split("\\:");
			if (sArr!=null&&sArr.length==2) {
				String[] bArr = sArr[0].split(" ");
				String[] aArr = sArr[1].split(" ");
				for (int i = 0; i < bArr.length; i++) {
					if (StringUtils.isNotBlank(bArr[i])) {
						int num = Integer.parseInt(bArr[i].trim());
						if (bMap.get(num)!=null) {
							bMap.put(num, bMap.get(num)+1);
						}
					}
				}
				for (int i = 0; i < aArr.length; i++) {
					if (StringUtils.isNotBlank(aArr[i])) {
						int num = Integer.parseInt(aArr[i].trim());
						if (aMap.get(num)!=null) {
							aMap.put(num, aMap.get(num)+1);
						}
					}
				}
			}
		}
		res.put("b", bMap);
		res.put("a", aMap);
		Utils.print(fileName+" line:"+list.size());
		res = XmlUtils.makeMap(res, true);
		return res;
	}
	public Map<String, Map<Integer, Integer>> tjHM4Aicai(String issueNo) throws IOException{
		Map<String, Map<Integer, Integer>> res = new HashMap<String, Map<Integer,Integer>>();
		List<String> list = XmlUtils.getHMData(issueNo);
		Map<Integer, Integer> bMap = new HashMap<Integer, Integer>();
		Map<Integer, Integer> aMap = new HashMap<Integer, Integer>();
		for (int i = 1; i <= 33; i++) {
			bMap.put(i, 0);
		}
		for (int i = 1; i <= 16; i++) {
			aMap.put(i, 0);
		}
		for (String s : list) {
			s = XmlUtils.replaceHanzi(s, ",");
			s = s.replaceAll(" ", "");
			String[] sArr = s.split("\\|");
			if (sArr!=null&&sArr.length==2) {
				String[] bArr = sArr[0].split(",");
				String[] aArr = sArr[1].split(",");
				for (int i = 0; i < bArr.length; i++) {
					if (StringUtils.isNotBlank(bArr[i])) {
						int num = Integer.parseInt(bArr[i].trim());
						bMap.put(num, bMap.get(num)+1);
					}
				}
				for (int i = 0; i < aArr.length; i++) {
					if (StringUtils.isNotBlank(aArr[i])) {
						int num = Integer.parseInt(aArr[i].trim());
						aMap.put(num, aMap.get(num)+1);
					}
				}
			}
		}
		res.put("b", bMap);
		res.put("a", aMap);
		Utils.print(issueNo+" line:"+list.size());
		res = XmlUtils.makeMap(res, true);
		return res;
	}
	
	public void writeTaobaoHMData(String game,String issueNo,String userName,boolean onsale){
		writeTaobaoHMData(game, issueNo, 2,userName,onsale);
		writeTaobaoHMData(game, issueNo, 1,userName,onsale);
	}
	
	public void writeTaobaoHMData(String game,String issueNo,int is_not_full,String userName,boolean onsale){
		String fullOrNotStr = getFullOrNotName(is_not_full);
		Utils.print("****TB "+game+" "+issueNo+"("+fullOrNotStr+") HMDetailLinks begin...");
		long b1 = System.currentTimeMillis();
		List<String> detailLinks = readDetailLinkTaobao(game, issueNo, is_not_full,userName,onsale);
		Utils.print("****TB "+game+" "+issueNo+"("+fullOrNotStr+") HMDetailLinks ok..."+detailLinks.size()+"...time:"+(System.currentTimeMillis()-b1)+"ms");
		int size = detailLinks.size();
		List<String> dataList = new ArrayList<String>();
		int i = 1;
		int totalRec = 0;
		Utils.print("****TB "+game+" "+issueNo+"("+fullOrNotStr+") HMData begin...");
		long b2 = System.currentTimeMillis();
		for (String li : detailLinks) {
			if(i%500==0){
				Utils.print("********TB "+game+" "+issueNo+"("+fullOrNotStr+") HMData..."+(i)+"->"+size);
			}
			String dataLink = getDataLinksHMTaobao(li);
			List<String> datas = readDataHMTaobao(dataLink);
			dataList.addAll(datas);
			i++;
		}
		Utils.print("****TB "+game+" "+issueNo+"("+fullOrNotStr+") HMData ok..."+dataList.size()+".time:"+(System.currentTimeMillis()-b2));
		Utils.print("****TB "+game+" "+issueNo+"("+fullOrNotStr+") HMData write begin...");
		long b3 = System.currentTimeMillis();
		StringBuilder cBuilder = new StringBuilder();
		for (String data : dataList) {
			totalRec++;
			//data = data.replaceAll(" ", "").replaceAll(" ", "");
			cBuilder.append(data).append("\r\n");
		}
		Utils.print("****TB "+game+" "+issueNo+"("+fullOrNotStr+") HMData write ok..."+dataList.size()+".time:"+(System.currentTimeMillis()-b3));
		XmlUtils.writeFile(cBuilder.toString(), "tb_"+issueNo+"_"+fullOrNotStr+".txt", false);
		Utils.print("TB issueNo: "+issueNo+"("+fullOrNotStr+") finished...users:"+detailLinks.size()+" records:"+totalRec);
	}
	
	
	public void writeAicaiSSQHMData(String issueNo){
		writeAicaiHMData("FC_SSQ", issueNo);
	}
	public void writeAicaiDLTHMData(String issueNo){
		writeAicaiHMData("TC_DLT", issueNo);
	}
	public void writeAicaiHMData(String game,String issueNo){
		int total = getTotalPageNum(game, issueNo);
		List<String> idList = new ArrayList<String>();
		for (int i = 1; i <= total; i++) {
			Map<String, String> idMap = readDataAicai(game,issueNo, i);
			idList.addAll(idMap.keySet());
		}
		StringBuilder cBuilder = new StringBuilder();
		int totalRec = 0;
		for (String id : idList) {
			List<String> list = readDataHMAicai(game,issueNo,id);
			for (String con : list) {
				totalRec++;
				con = con.replaceAll(" ", "");
				cBuilder.append(con).append("\r\n");
			}
		}
		XmlUtils.writeFile(cBuilder.toString(), issueNo+".txt", false);
		Utils.print("issueNo: "+issueNo+" finished...users:"+idList.size()+" records:"+totalRec);
	}
	
	private List<String> readDataHMAicai(String game,String issueNo,String id) {
		//String url = "http://www.aicai.com/hemai-"+id+".html";
			String url = "http://www.aicai.com/hm/hmdetail!queryPlanDetailContent.jhtml?"
					+ "planNo="+id+"&game="+game+"&issueNo="+issueNo+"&selectType=MANUAL";
		Connection conn = Jsoup.connect(url);
		Document doc = null;
		try {
			doc = conn.get();
		} catch (HttpStatusException e) {
			e.printStackTrace();
			//XmlUtils.writeLogs(" number: "+issueNo+" is illegal.", CPDataService.class, null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		List<String> resList = new ArrayList<String>();
		Elements div = doc.getElementsByAttributeValue("class", "facont_table");
		if (div.size()>0) {
			Elements tbody =div.get(0).getElementsByTag("tbody");
			Elements tr1 = tbody.get(0).getElementsByAttributeValue("class", "tpleft");
			for (Element e : tr1) {
				//System.out.println(e.ownText());
				resList.add(e.ownText());
			}
		}
		return resList;
	}
	private int getSSQNum(String issueNo){
		return getTotalPageNum("FC_SSQ", issueNo);
	}
	private int getDLTNum(String issueNo){
		return getTotalPageNum("TC_DLT", issueNo);
	}
	private int getTotalPageNum(String game,String issueNo){
//		String url = "http://www.aicai.com/hemai/tcdlt/";
		
		String url = "http://www.aicai.com/hm/hmlottery!hemailPlan.jhtml?&game="+game+""
				+"&issueNo="+issueNo+"&groupSqlBuildType=TOP&version=2&encryptBoolean=true"
				+"&hmActionPageUrl=/hm/hmlottery!hemailPlan.jhtml&current_page=1&showPageSize=30";
		Connection conn = Jsoup.connect(url);
		
		Document doc = null;
		try {
			doc = conn.get();
		} catch (HttpStatusException e) {
			//XmlUtils.writeLogs(" number: "+issueNo+" is illegal.", CPDataService.class, null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Element e = doc.getElementById("jq_page_total");
		String num = e.attr("value");
		return Integer.parseInt(num);
	}
	private Map<String, String> getSSQData(String issueNo,int current_page){
		return readDataAicai("FC_SSQ", issueNo,current_page);
	}
	private Map<String, String> getDLTData(String issueNo,int current_page){
		return readDataAicai("TC_DLT", issueNo,current_page);
	}
	private Map<String, String> readDataAicai(String game,String issueNo,int current_page) {
		String url = "http://www.aicai.com/hm/hmlottery!hemailPlan.jhtml?&game="+game+""
				+"&issueNo="+issueNo+"&groupSqlBuildType=TOP&version=2&encryptBoolean=true"
				+"&hmActionPageUrl=/hm/hmlottery!hemailPlan.jhtml&current_page="+current_page+"&showPageSize=30";
		Connection conn = Jsoup.connect(url);
		Document doc = null;
		try {
			doc = conn.get();
		} catch (HttpStatusException e) {
			XmlUtils.writeLogs(" number: "+issueNo+" is illegal.", CPDataService.class, null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String, String> resMap = new HashMap<String, String>();
		Elements elements = doc.getElementsByAttributeValue("class", "table_list");
		Elements tr = elements.get(0).getElementsByTag("tr");
		for (Element element : tr) {
			String id = element.attr("id");
			String id1 = id.substring(id.lastIndexOf("_")+1);
			resMap.put(id1, id1);
		}
		return resMap;
	}
	public Map<String,Object> lastIssuoInfo(String game){
		String url = "http://caipiao.taobao.com//lottery/ajax/get_united_list.htm" +
				"?page="+dataLinkPageNo+"&issue=&lotteryType="+game+"&playType=&commission_rate=-1"+
				"&confidential=1&united_fee=0-0&is_not_full=1&creator=&sort_obj=bonusRatio&sort=desc&change_sort=false"+
				"&lowAmount=0&highAmount=0&from=&onsale=&t="+System.currentTimeMillis();
		Connection conn = Jsoup.connect(url);
		Document doc = null;
		int i = 1;
		while (doc == null) {
			try {
				doc = conn.get();
			} catch (IOException e) {
				//e.printStackTrace();
				++i;
				if (i>10) {
					Utils.print("********("+e.getMessage()+")Try to read 10("+i+") times.");
//					System.exit(0);
				}
				conn = Jsoup.connect(url);
			}
		}
		String resultStr = doc.text();
		Map<String,Object> infoMap = new HashMap<String, Object>();
		JSONObject object = JSONObject.fromObject("{\""+resultStr.substring(resultStr.indexOf("totalItem")));
		infoMap.put("totalItem", object.get("totalItem"));
		infoMap.put("lotteryType", object.get("lotteryType"));
		infoMap.put("totalPage", object.get("totalPage"));
		List<String> issueList = new ArrayList<String>();
		JSONArray arr = object.getJSONArray("issueList");
		for (Object object2 : arr) {
			JSONObject object3 = (JSONObject) object2;
			issueList.add(object3.getString("issue"));
			if (object3.getBoolean("onsale")) {
				infoMap.put("onsaleIssueNo", object3.get("issue"));
			}
		}
		infoMap.put("issueList", issueList);
		return infoMap;
	}
	private int dataLinkPageNo = 1;
	/**
	 * @param game
	 * @param issueNo
	 * @param is_not_full
	 * @return detailLink 详情界面链接
	 */
	private  List<String> readDetailLinkTaobao(String game,String issueNo,int is_not_full,String userName,boolean onsale){
		String fullOrNotStr = getFullOrNotName(is_not_full);
		/*--ssq 未满员 不限佣金 内容公开（返奖率 排序 sort_obj=unitedRoi）
		http://caipiao.taobao.com/lottery/ajax/get_united_list.htm?page=1&issue=&lotteryType=SSQ&playType=&commission_rate=-1
		&confidential=1&united_fee=0-0&is_not_full=1&creator=&sort_obj=process&sort=desc&change_sort=false
		&lowAmount=0&highAmount=0&from=&onsale=&t=1394339345261*/
		//is_not_full 2:已满员   1:未满员 //bonusRatio  返奖率
		String url = "http://caipiao.taobao.com//lottery/ajax/get_united_list.htm" +
				"?page="+dataLinkPageNo+"&issue="+issueNo+"&lotteryType="+game+"&playType=&commission_rate=-1"+
				"&confidential=1&united_fee=0-0&is_not_full="+is_not_full+"&creator="+userName+"&sort_obj=bonusRatio&sort=desc&change_sort=false"+
				"&lowAmount=0&highAmount=0&from=&onsale="+onsale+"&t="+System.currentTimeMillis();
		Connection conn = Jsoup.connect(url);
		Document doc = null;
		int i = 1;
		while (doc == null) {
			try {
				doc = conn.get();
			} catch (IOException e) {
				//e.printStackTrace();
				++i;
				if (i>10) {
					Utils.print("********("+e.getMessage()+")Try to read 10("+i+") times.");
//					System.exit(0);
				}
				conn = Jsoup.connect(url);
			}
		}
		String resultStr = doc.text();
		List<String> resList = new ArrayList<String>();
		int totalPage = Integer.parseInt(resultStr.substring(resultStr.indexOf("totalPage")+11, resultStr.lastIndexOf("sale")-2));
		resList.addAll(getDetailLikes(resultStr));
		if (dataLinkPageNo%30==0) {
			Utils.print("********TB "+game+" "+issueNo+"("+fullOrNotStr+") HMDetailLinks pageNo:"+dataLinkPageNo+" totalPage:"+totalPage);
		}
		dataLinkPageNo++;
		if (totalPage>1&&totalPage>=dataLinkPageNo) {
			resList.addAll(readDetailLinkTaobao(game, issueNo, is_not_full,userName,onsale));
		}else {
			dataLinkPageNo = 1;
		}
		return resList;
	}
	
	public List<String> getDetailLikes(String ss){
		List<String> list = new ArrayList<String>();
		if (ss.indexOf("detailLink")>-1) {
			String detailLink = ss.substring(ss.indexOf("detailLink")+13, ss.indexOf("title")-3);
			list.add(detailLink);
		}
		if (ss.length()>0) {
			if (ss.indexOf("detailLink")>-1) {
				ss = ss.substring(ss.indexOf("title")+3);
				list.addAll(getDetailLikes(ss));
			}
		}
		return list;
	}
	
	private String getDataLinksHMTaobao(String detailLink){
		Connection conn = Jsoup.connect(detailLink);
		Document doc = null;
		int i = 1;
		while (doc == null) {
			try {
				doc = conn.get();
			} catch (IOException e) {
				//e.printStackTrace();
				++i;
				if (i>10) {
					Utils.print("********("+e.getMessage()+")Try to read 10("+i+") times.");
//					System.exit(0);
				}
				conn = Jsoup.connect(detailLink);
			}
		}
		List<String> resList = new ArrayList<String>();
		Elements tds = doc.getElementsByAttributeValue("class", "scheme-content");
		String dataLink = "";
		if (tds.size()>0) {
			dataLink = tds.get(0).getElementsByTag("iframe").get(0).attr("src");
		}
		return dataLink;
	}
	
	
	private int hmdatapageNo = 1;
	private List<String> readDataHMTaobao(String dataLink){
		
		/*String url = "http://caipiao.taobao.com/lottery/order/united_order_detail_szc.htm?" +
				"_tb_token_=e1bd7335514eb&page="+pageNo +
				"&tb_united_id=" + tb_united_id +
				"&lottery_type_id=1" +
				"&issue_id="+issue_id+"&db_type=0&is_history=false";*/
		String url = "http://caipiao.taobao.com/lottery/order/"+dataLink+"&page="+hmdatapageNo;
		Connection conn = Jsoup.connect(url);
		Document doc = null;
		int i = 1;
		while (doc == null) {
			try {
				doc = conn.get();
			} catch (IOException e) {
				//e.printStackTrace();
				++i;
				if (i>10) {
					Utils.print("********("+e.getMessage()+")Try to read 10("+i+") times.");
//					System.exit(0);
				}
				conn = Jsoup.connect(url);
			}
		}
		List<String> resList = new ArrayList<String>();
		Element div = doc.getElementById("select-detail");
		Elements tds = div.getElementsByAttributeValue("class", "td2");
		if (tds!=null&&tds.size()>0) {
			for (Element e : tds) {
				String data = e.ownText();
				if (data.lastIndexOf("单式")>-1) {
					data = data.substring(0, data.lastIndexOf("单式"));
				}
				if (data.lastIndexOf("胆拖")>-1) {
					data = data.substring(0, data.lastIndexOf("胆拖"));
				}
				if (data.lastIndexOf("复式")>-1) {
					data = data.substring(0, data.lastIndexOf("复式"));
				}
				if (data.lastIndexOf("普通")>-1) {
					data = data.substring(0, data.lastIndexOf("普通"));
				}
				data = data.replaceAll("\\)", " ");
				data = data.replaceAll("\\(", " ");
				//System.out.println(data);
				resList.add(data);
			}
		}
		Elements pageEs = doc.getElementsByAttributeValue("class", "page-info");
		if (pageEs.size()>0) {
			String pageInfo = pageEs.get(0).ownText();
			int pages = Integer.parseInt(pageInfo.substring(pageInfo.indexOf("/")+1));
			hmdatapageNo++;
			if (pages>1&&pages>=hmdatapageNo) {
				resList.addAll(readDataHMTaobao(dataLink));
			}else {
				hmdatapageNo = 1;
			}
		}
		return resList;
	}
	
	private String getFullOrNotName(int is_not_full){
		String fullOrNotStr = "";
		switch (is_not_full) {
		case 1:
			fullOrNotStr = "notfull";
			break;
		case 2:
			fullOrNotStr = "full";
			break;
		default:
			break;
		}
		return fullOrNotStr;
	}
	
}
class ServiceThread implements Runnable{
	private String game;
	private String issueNo;
	private boolean onsale;
	private String userName;
	public ServiceThread(String game,String issueNo,String userName,boolean onsale){
		this.game = game;
		this.issueNo = issueNo;
		this.onsale = onsale;
		this.userName = userName;
	}
	
	@Override
	public void run() {
		//synchronized(CPHMService.class){
		CPHMTBService service = new CPHMTBService();
		service.writeTaobaoHMData(game, issueNo,userName, onsale);
		//}
	}
	
}
 

